package com.gopay.bis.ccb.ebank.operation;


import CCBSign.RSASig;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.ccb.ebank.pay.CcbB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.ICertHelper;

/**
 * 建行B2C签名验签实现类
 * 
 * @CCBCerthelper.java
 * @author WangHongFei
 */
public class CcbB2cCerthelper implements ICertHelper {
	
	public String sign(BaseMessage message) throws GopayException{
		try{
			CcbB2cMessage b2cMessage = (CcbB2cMessage) message;
			BankLogUtils.printB2CSignPlainLog(b2cMessage);
			RSASig rsaHelperB2c = new RSASig();
			rsaHelperB2c.setPublicKey(b2cMessage.getPublicKey());
			String digest = rsaHelperB2c.generateDigest(b2cMessage.getPlainMessage());
			BankLogUtils.printB2CSignSignatureLog(b2cMessage, digest);
			return digest;
		}catch(Exception e){
			BankLogUtils.printB2CSignExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
		}
	}

	public Object verify(BaseMessage message) throws GopayException{
		try{
			CcbB2cMessage b2cMessage = (CcbB2cMessage) message;
			RSASig rsaHelperB2c = new RSASig();
			rsaHelperB2c.setPublicKey(b2cMessage.getPublicKey());
			BankLogUtils.printB2CVerifyPlainLog(b2cMessage);
			BankLogUtils.printB2CVerifySignatureLog(b2cMessage, b2cMessage.getSignMessage() + " orderId: " + b2cMessage.getOrderId() + ": " + rsaHelperB2c.getPublicKey());			
			boolean verifyRes = rsaHelperB2c.verifySigature(b2cMessage.getSignMessage(),b2cMessage.getPlainMessage());
			BankLogUtils.printB2CVerifyResultLog(message, verifyRes);
			return verifyRes;
		}catch(Exception e){
			BankLogUtils.printB2CVerifyExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
		}	
	}

}
